package com.yohann.eduservice.controller;


import com.yohann.commonutils.result.R;
import com.yohann.eduservice.entity.EduChapter;
import com.yohann.eduservice.entity.vo.ChapterVo;
import com.yohann.eduservice.service.EduChapterService;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-08-08
 */
@RestController
@RequestMapping("/edu-service/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    /**
     * 通过课程id获取章节信息
     * @param courseId
     * @return
     */
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = chapterService.getChapterVideoById(courseId);

        return R.ok().data("allChapterVideo",list);
    }

    /**
     * 添加章节信息
     * @param eduChapter
     * @return
     */
    @PostMapping("add")
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);

        return R.ok();
    }

    /**
     * 通过id获取章节信息
     * @param id
     * @return
     */
    @GetMapping("getChapter/{id}")
    public R getChapter(@PathVariable String id){
        return R.ok().data("chapter",chapterService.getById(id));
    }

    /**
     * 修改章节信息
     * @param eduChapter
     * @return
     */
    @PostMapping("update")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);

        return R.ok();
    }

    /**
     * 删除章节
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R deleteChapter(@PathVariable String id){
        Boolean deleteChapter = chapterService.deleteChapter(id);
        if (deleteChapter){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

